package com.csdj.server.controller;


import com.csdj.server.pojo.Admin;
import com.csdj.server.pojo.RespBean;
import com.csdj.server.pojo.Role;
import com.csdj.server.service.IAdminService;
import com.csdj.server.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xyx
 * @since 2022-12-12
 */
@RestController
@RequestMapping("/system/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;

    @Autowired
    private IRoleService iRoleService;

    @ApiOperation(value = "模糊查询操作员信息")
    @GetMapping("/")
    public List<Admin> getAllAdmins(String keywords){
        return adminService.getAllAdmins(keywords);
    }

    @ApiOperation(value = "修改操作员信息")
    @PutMapping("/")
    public RespBean updateAdmins(@RequestBody Admin admin){
        if(adminService.updateById(admin)){
            return RespBean.success("修改成功!");
        }
        return RespBean.error("修改失败!");
    }

    @ApiOperation(value = "删除操作员信息")
    @DeleteMapping("/{id}")
    public RespBean deleteAdmins(@PathVariable("id")Integer id){
        if(adminService.removeById(id)){
            return RespBean.success("删除成功!");
        }
        return RespBean.error("删除失败!");
    }

    @ApiOperation(value = "更新操作员角色")
    @PutMapping("/role")
    public RespBean updateAdminRole(Integer adminId, Integer[] rids){
        return adminService.updateAdminRole(adminId, rids);
    }

    @ApiOperation(value = "获取角色列表")
    @GetMapping("/roles")
    public List<Role> getRoles(){
        return iRoleService.list();
    }
}
